package org.example.cdpcrpbackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.example.cdpcrpbackend.controller.dto.ApplyCrp;
import org.example.cdpcrpbackend.controller.dto.ScoreRecord1;
import org.example.cdpcrpbackend.domain.ScoreRecord;
import org.example.cdpcrpbackend.domain.Student;
import org.example.cdpcrpbackend.mapper.ScoreRecordMapper;
import org.example.cdpcrpbackend.mapper.StudentMapper;
import org.example.cdpcrpbackend.service.ScoreRecordService;
import org.example.cdpcrpbackend.util.MyType;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ScoreRecordServiceImpl extends ServiceImpl<ScoreRecordMapper, ScoreRecord> implements ScoreRecordService {
    private final StudentMapper studentMapper;
    private final ScoreRecordMapper scoreRecordMapper;
    @Override
    public boolean apply(ApplyCrp applyCrp) {
        ScoreRecord scoreRecord = new ScoreRecord();
        BeanUtil.copyProperties(applyCrp, scoreRecord);
        scoreRecord.setApplyTime(LocalDateTime.now());
        scoreRecord.setState("申请中");
        scoreRecord.setMethod(MyType.M5);     // 学生手动加分

        // 获取学生学期
        Student student = studentMapper.selectById(applyCrp.getStudentId());
        scoreRecord.setSemester(student.getSemester());
        System.out.println(applyCrp);
        System.out.println(scoreRecord);
        return save(scoreRecord);
    }

    @Override
    public List<ScoreRecord1> listByPage(String key, String classId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return scoreRecordMapper.listByCondition(key, classId);
    }

    @Override
    public ScoreRecord1 crpInfoById(String id) {
        return scoreRecordMapper.crpInfoById(id);
    }
}
